﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.BLL;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery
{
    public partial class FrmRuleGroup : Form
    {
        #region Private Variables

        decimal _CapitalRate, _FirstDay, _HeadCapital, _TailCapital;
        string _HeadOrderBy, _HeadDirection;
        string _TailOrderBy, _TailDirection;
        bool _IsHeadLoaded, _IsTailLoaded;
        Dictionary<string, decimal> _HeadValueList, _TailValueList;
        Dictionary<string, decimal> _HeadFirstValueList, _TailFirstValueList;
        List<GroupValue> _GroupValueList;

        #endregion Private Variables

        #region Private Methods

        private void FrmRuleGroup_Load(object sender, EventArgs e)
        {
            _HeadValueList = new Dictionary<string, decimal>();
            _TailValueList = new Dictionary<string, decimal>();
            _HeadFirstValueList = new Dictionary<string, decimal>();
            _TailFirstValueList = new Dictionary<string, decimal>();

            _HeadOrderBy = GroupColumns.Order.ToString();
            _HeadDirection = "ASC";
            _TailOrderBy = GroupColumns.Order.ToString();
            _TailDirection = "ASC";

            _IsHeadLoaded = true;
            _IsTailLoaded = false;

            LoadSetting();
            LoadSettingValue();
            LoadGroupValue();

            LoadRule();
            LoadHeadValue();
            LoadHeadRuleGroup();
        }

        private void LoadSetting()
        {
            try
            {
                Setting setting = SettingBLL.GetSetting(KZ_Resource.Setting_CapitalRate);
                decimal capitalRate = Utility.DecimalParse(setting.SettingValue);
                _CapitalRate = capitalRate / 100;
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadSetting", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void LoadSettingValue()
        {
            try
            {
                SettingValue settingValue = SettingValueBLL.GetSettingValueFirstDay();
                _FirstDay = settingValue.Value;
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadSettingValue", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void LoadGroupValue()
        {
            try
            {
                _GroupValueList = GroupValueBLL.GetGroupValueList();
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadSetting", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void LoadRule()
        {
            try
            {
                Entities.Rule rule = RuleBLL.GetRule(RuleID);
                txtHeadCapital.Text = Utility.CurrencyParse(rule.HeadCapital, Global.Culture);
                txtHeadActualCapital.Text = Utility.CurrencyParse(rule.HeadActualCapital, Global.Culture);
                txtTailCapital.Text = Utility.CurrencyParse(rule.TailCapital, Global.Culture);
                txtTailActualCapital.Text = Utility.CurrencyParse(rule.TailActualCapital, Global.Culture);
                txtHeadWinAmount.Text = Utility.CurrencyParse(rule.HeadWinAmount, Global.Culture);
                txtTailWinAmount.Text = Utility.CurrencyParse(rule.TailWinAmount, Global.Culture);
                txtHeadRevenue.Text = Utility.CurrencyParse(rule.HeadWinAmount - rule.HeadActualCapital, Global.Culture);
                txtTailRevenue.Text = Utility.CurrencyParse(rule.TailWinAmount - rule.TailActualCapital, Global.Culture);
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadSetting", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void LoadHeadValue()
        {
            try
            {
                dtgHeadValue.Rows.Clear();
                dtgHeadValue.Rows.Add(100);
                for (int i = 0; i < 100; i++)
                {
                    _HeadValueList.Add(i.ToString("00"), 0);
                    _HeadFirstValueList.Add(i.ToString("00"), 0);
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadHeadValue", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void LoadTailValue()
        {
            try
            {
                dtgTailValue.Rows.Clear();
                dtgTailValue.Rows.Add(100);
                for (int i = 0; i < 100; i++)
                {
                    _TailValueList.Add(i.ToString("00"), 0);
                    _TailFirstValueList.Add(i.ToString("00"), 0);
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadTailValue", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void LoadHeadRuleGroup()
        {
            try
            {
                string ruleID = "'" + RuleID + "'";
                List<RuleGroup> headList = RuleGroupBLL.GetRuleGroupList(ruleID, KZ_Resource.Head, _HeadOrderBy, _HeadDirection);

                int tempCount;
                int count = headList.Count;

                dtgHead.Rows.Clear();
                dtgHead.Rows.Add(count);
                List<GroupValue> groupValueList;
                _HeadCapital = 0;
                for (int i = 0; i < count; i++)
                {
                    dtgHead[colHeadOrder.Index, i].Value = headList[i].GroupID;
                    dtgHead[colHeadDay.Index, i].Value = headList[i].Day;
                    dtgHead[colHeadAmount.Index, i].Value = headList[i].Value;

                    _HeadCapital += headList[i].Day * Global.NumberOfGroup;

                    groupValueList = new List<GroupValue>(_GroupValueList.Where(gv => gv.GroupID == headList[i].GroupID));
                    tempCount = groupValueList.Count;
                    for (int j = 0; j < tempCount; j++)
                    {
                        _HeadValueList[groupValueList[j].Value] += headList[i].Value;
                        _HeadFirstValueList[groupValueList[j].Value] += headList[i].FirstDayAmount;
                    }
                }

                HeadValueList = _HeadValueList;
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadHeadRuleGroup", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void LoadTailRuleGroup()
        {
            try
            {
                string ruleID = "'" + RuleID + "'";
                List<RuleGroup> tailList = RuleGroupBLL.GetRuleGroupList(ruleID, KZ_Resource.Tail, _TailOrderBy, _TailDirection);

                int tempCount;
                int count = tailList.Count;
                dtgTail.Rows.Clear();
                dtgTail.Rows.Add(count);
                List<GroupValue> groupValueList;
                _TailCapital = 0;
                for (int i = 0; i < count; i++)
                {
                    dtgTail[colTailOrder.Index, i].Value = tailList[i].GroupID;
                    dtgTail[colTailDay.Index, i].Value = tailList[i].Day;
                    dtgTail[colTailAmount.Index, i].Value = tailList[i].Value;

                    _TailCapital += tailList[i].Day * Global.NumberOfGroup;

                    groupValueList = new List<GroupValue>(_GroupValueList.Where(gv => gv.GroupID == tailList[i].GroupID));
                    tempCount = groupValueList.Count;
                    for (int j = 0; j < tempCount; j++)
                    {
                        _TailValueList[groupValueList[j].Value] += tailList[i].Value;
                        _TailFirstValueList[groupValueList[j].Value] += tailList[i].FirstDayAmount;
                    }
                }

                TailValueList = _TailValueList;
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadTailRuleGroup", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void dtgHead_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex != Column1.Index && e.ColumnIndex != colHeadAmount.Index)
            {
                string orderBy = dtgHead.Columns[e.ColumnIndex].Name.Replace("colHead", String.Empty);
                if (orderBy == _HeadOrderBy)
                {
                    _HeadDirection = _HeadDirection == "ASC" ? "DESC" : "ASC";
                }
                else
                {
                    _HeadDirection = "ASC";
                    _HeadOrderBy = orderBy;
                }
                LoadHeadRuleGroup();
            }
        }

        private void dtgTail_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex != Column9.Index && e.ColumnIndex != colTailAmount.Index)
            {
                string orderBy = dtgTail.Columns[e.ColumnIndex].Name.Replace("colTail", String.Empty);
                if (orderBy == _TailOrderBy)
                {
                    _TailDirection = _TailDirection == "ASC" ? "DESC" : "ASC";
                }
                else
                {
                    _TailDirection = "ASC";
                    _TailOrderBy = orderBy;
                }
                LoadTailRuleGroup();
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab.Name == tbpHead.Name)
            {
                if (!_IsHeadLoaded)
                {
                    if (!_IsTailLoaded) { LoadRule(); }
                    LoadHeadValue();
                    LoadHeadRuleGroup();

                    _IsHeadLoaded = true;
                }
            }
            else
            {
                if (!_IsTailLoaded)
                {
                    if (!_IsHeadLoaded) { LoadRule(); }
                    LoadTailValue();
                    LoadTailRuleGroup();

                    _IsTailLoaded = true;
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            _HeadValueList.Clear();
            _TailValueList.Clear();
            _HeadFirstValueList.Clear();
            _TailFirstValueList.Clear();

            _IsHeadLoaded = false;
            _IsTailLoaded = false;

            tabControl1_SelectedIndexChanged(tabControl1, null);
        }

        #endregion Private Methods

        #region Constructors

        public FrmRuleGroup()
        {
            InitializeComponent();

            colHeadAmount.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colTailAmount.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
        }

        #endregion Constructors

        #region Public Members

        public string RuleID { get; set; }

        public Dictionary<string, decimal> HeadValueList
        {
            set
            {
                int count = value.Count;
                string temp;
                decimal total = 0;
                for (int i = 0; i < count; i++)
                {
                    temp = i.ToString("00");
                    total += value[temp] - _FirstDay * Global.NumberOfValue;
                    dtgHeadValue[colHeadValue.Index, i].Value = temp;
                    dtgHeadValue[colHeadValueAmount.Index, i].Value = Utility.CurrencyParse(value[temp], Global.Culture);
                    dtgHeadValue[colHeadCapital.Index, i].Value = Utility.CurrencyParse(value[temp] - _HeadFirstValueList[temp], Global.Culture);
                }

                txtHeadTotalAmount.Text = Utility.CurrencyParse(total, Global.Culture);
            }
        }

        public Dictionary<string, decimal> TailValueList
        {
            set
            {
                int count = value.Count;
                string temp;
                decimal total = 0;
                for (int i = 0; i < count; i++)
                {
                    temp = i.ToString("00");
                    total += value[temp] - _FirstDay * Global.NumberOfValue;
                    dtgTailValue[colTailValue.Index, i].Value = temp;
                    dtgTailValue[colTailValueAmount.Index, i].Value = Utility.CurrencyParse(value[temp], Global.Culture);
                    dtgTailValue[colTailCapital.Index, i].Value = Utility.CurrencyParse(value[temp] - _TailFirstValueList[temp], Global.Culture);
                }

                txtTailTotalAmount.Text = Utility.CurrencyParse(total, Global.Culture);
            }
        }

        #endregion Public Members
    }
}